package com.hadwinling.blog.mvc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author HadwinLing
 * @version V1.0
 * @Package com.hadwinling.myblog.mvc
 * @date 2020/7/3 9:25
 * 实现自定义mvc配置
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private UriInterceptor uriInterceptor;

    @Autowired
    private AdminUriInterceptor adminUriInterceptor;
    /**
     * 给指定页面添加控制器
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //给指定的试图田间url请求映射
        //http://localhost:8080/blog/admin/
        registry.addViewController("/user/").setViewName("/UserCentre/userCentral");
        registry.addViewController("/admin/").setViewName("/AdminCenter/login");
        registry.addViewController("/admin/login").setViewName("/AdminCenter/login");
        registry.addViewController("/admin/toLoginPage").setViewName("/AdminCenter/login");

    }

    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(uriInterceptor)
                .addPathPatterns("/user/**") //表示拦截所有请求
                .excludePathPatterns("/user/login","/")  //不拦截哪些请求
                .excludePathPatterns("/user/register","/")  //不拦截哪些请求
                .excludePathPatterns("/js/**") //静态资源不拦截
                .excludePathPatterns("/css/**")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/dist/**")
                .excludePathPatterns("/img/**")
                .excludePathPatterns("/plugins/**")
                .excludePathPatterns("/bower_components/**");
        registry.addInterceptor(adminUriInterceptor)
                .addPathPatterns("/admin/**") //表示拦截所有请求
                .excludePathPatterns("/admin/login") //不拦截哪些请求
                .excludePathPatterns("/js/**") //静态资源不拦截
                .excludePathPatterns("/css/**")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/dist/**")
                .excludePathPatterns("/img/**")
                .excludePathPatterns("/plugins/**")
                .excludePathPatterns("/bower_components/**");
    }
}
